package com.example.weatherforcast;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.Custom.LocationCity;
import com.example.Utils.DBUtil;
import com.example.application.MyApplication;
import com.example.entity.Sk;
import com.example.entity.Today;
import com.example.entity.Weather;
import com.google.gson.Gson;

public class CityActivity extends Activity implements OnClickListener {
	LocationManager lm;
	LocationCity l;
	Button btn_back, btn_manager, btn_add;
	ListView ll_add;
	public final static int CITY = 0X01;
	private List<String> citylist;
	MyAdapter adapter;
	SharedPreferences mpreference;
	Editor editor;
	private static boolean SHOW = false;
	private List<String> check = null;
	private List<Integer> wlist = null;
	private List<String> tlist;
	private MyApplication mApplication;
	private SQLiteDatabase db = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addcity);
		initview();

		listen();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void initview() {
		// TODO Auto-generated method stub
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_manager = (Button) findViewById(R.id.btn_manager);
		btn_add = (Button) findViewById(R.id.btn_add);
		ll_add = (ListView) findViewById(R.id.ll_add);
	}

	private void listen() {
		// TODO Auto-generated method stub
		btn_add.setOnClickListener(this);
		btn_back.setOnClickListener(this);
		btn_manager.setOnClickListener(this);
		ll_add.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (SHOW) {
					if (check.contains(citylist.get(position))) {
						check.remove(citylist.get(position));
						view.setBackgroundColor(Color.WHITE);

					} else {
						check.add(citylist.get(position));
						view.setBackgroundColor(Color.BLUE);
					}

				} else {
					String st = citylist.get(position);
					List<String> slist = new ArrayList<String>();
					for (int i = 0; i < citylist.size(); i++) {
						slist.add(citylist.get(i));

					}
					slist.remove(position);
					citylist.clear();
					citylist.add(st);
					wlist.clear();
					tlist.clear();
					for (int i = 0; i < slist.size(); i++) {
						citylist.add(slist.get(i));
					}
					wlist = setDataweather(citylist);
					tlist = setDatatemp(citylist);
					editor.clear();
					editor.commit();
					for (int i = 0; i < citylist.size(); i++) {

						editor.putString((i + 1) + "", citylist.get(i));
						editor.commit();

					}
					editor.putInt("count", citylist.size());
					editor.commit();
					finish();
				}
			}
		});
	}

	private void initData() {
		// TODO Auto-generated method stub
		mApplication = MyApplication.getInstance();
		db = mApplication.getDb();
		wlist = new ArrayList<Integer>();
		tlist = new ArrayList<String>();
		citylist = new ArrayList<String>();
		check = new ArrayList<String>();
		adapter = new MyAdapter();

		mpreference = getSharedPreferences("city", MODE_PRIVATE);
		editor = mpreference.edit();
		int count = mpreference.getInt("count", -1);
		for (int i = 0; i < count; i++) {
			citylist.add(mpreference.getString(i + 1 + "", "上海市"));
			// setData(mpreference.getString(i + 1 + "", "上海市"));

		}
		wlist = setDataweather(citylist);
		tlist = setDatatemp(citylist);

		ll_add.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			Intent intent = new Intent(this, AddActivity.class);
			// intent.putExtra("ada", "dddd");
			// startActivityForResult(intent, CITY);
			startActivity(intent);

			break;
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_manager:
			btn_manager.setText("删除");
			ll_add.setBackgroundColor(Color.GRAY);
			if (SHOW) {
				btn_manager.setText("编辑");
				ll_add.setBackgroundColor(Color.WHITE);
				for (int i = (citylist.size() - 1); i > -1; i--) {

					for (int j = 0; j < check.size(); j++) {
						if (citylist.get(i).equals(check.get(j))) {
							citylist.remove(i);
							editor.remove(i + 1 + "");
							editor.commit();

							break;
						}
					}
				}
				editor.putInt("count", citylist.size());
				editor.commit();
				wlist = setDataweather(citylist);
				tlist = setDatatemp(citylist);
				adapter.notifyDataSetChanged();
			}
			SHOW = !SHOW;

			check.clear();

			break;

		default:
			break;
		}

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return citylist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return citylist.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = convertView;
			Hondle hondle = null;
			TextView tv_citylist, tv_citylisttemp;
			ImageView iv_citylist;
			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.list_city, null);
				tv_citylist = (TextView) view.findViewById(R.id.tv_citylist);
				tv_citylisttemp = (TextView) view
						.findViewById(R.id.tv_citylisttemp);
				iv_citylist = (ImageView) view.findViewById(R.id.iv_citylist);
				hondle = new Hondle();

				hondle.tv_citylisttemp = tv_citylisttemp;
				hondle.iv_citylist = iv_citylist;
				hondle.tv_citylist = tv_citylist;

				view.setTag(hondle);
			} else {
				hondle = (Hondle) view.getTag();
				tv_citylisttemp = hondle.tv_citylisttemp;
				tv_citylist = hondle.tv_citylist;
				iv_citylist = hondle.iv_citylist;

			}
			tv_citylist.setText(citylist.get(position));
			// if (wlist.size()!=position) {
			iv_citylist.setImageResource(wlist.get(position));
			// }
			// if (wlist.size()!=position) {
			tv_citylisttemp.setText(tlist.get(position));
			// }

			return view;
		}

	}

	class Hondle {
		ImageView iv_citylist, iv_citylist_del;
		TextView tv_citylist, tv_citylisttemp;
	}

	private List<String> setDatatemp(List<String> citylist) {
		tlist.clear();
		for (int i = 0; i < citylist.size(); i++) {
			String weather = null;

			Today today = null;
			Sk sk = null;
			String data = DBUtil.querySingle(db, citylist.get(i));

			if (data != null) {
				Gson gson = new Gson();
				Weather w = gson.fromJson(data, Weather.class);
				today = w.getResult().getToday();
				sk = w.getResult().getSk();

				if (sk != null) {

					String st = today.getTemperature();

					tlist.add(st);
				} else {
					// 获取天气信息失败也要放进去，否则数据越界
					tlist.add("获取天气失败");
				}
			} else {
				// 获取天气信息失败也要放进去，否则数据越界
				tlist.add("获取天气失败");
			}
		}
		return tlist;
	}

	private List<Integer> setDataweather(List<String> citylist) {
		wlist.clear();
		for (int i = 0; i < citylist.size(); i++) {
			String weather = null;

			Today today = null;
			Sk sk = null;
			String data = DBUtil.querySingle(db, citylist.get(i));

			if (data != null) {
				Gson gson = new Gson();
				Weather w = gson.fromJson(data, Weather.class);
				today = w.getResult().getToday();
				sk = w.getResult().getSk();

				if (sk != null) {
					weather = today.getWeather();

					if (weather.contains("转")) {
						weather = weather.substring(0, weather.indexOf("转"));
					}

					if (weather != null) {
						if (weather.equals("晴")) {
							wlist.add(R.drawable.ww0);
						}

						if (weather.equals("多云")) {
							wlist.add(R.drawable.ww1);
						}

						if (weather.contains("雨")) {
							wlist.add(R.drawable.ww10);
						}

						if (weather.contains("雪")) {
							wlist.add(R.drawable.ww17);
						}else {
							// 看看这里面。需要设置个默认的天气或者用个获取天气失败的图标
							wlist.add(R.drawable.ww17);
						}
					}
				}
			} else {
				// 看看这里面。需要设置个默认的天气或者用个获取天气失败的图标
				wlist.add(R.drawable.ww17);
			}
		}
		return wlist;
	}
}